//
//  LGPatientSportsController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/9.
//

#import "LGPatientSportsController.h"
//运动评估
#import "LGSportsEvaRecordController.h"
//运动方案详情
#import "LGPatientSportsDetailController.h"
//训练记录
#import "LGSportsTrainRecordController.h"
//报告中心
#import "LGReportingCenterController.h"
//评估结果
#import "LGGetWebViewController.h"
//统计图
#import "LGPatientSportsChartView.h"
#import "LGPatientSportsEvaCell.h"
#import "LGEvaluationReportModel.h"
#import "SVGKit.h"
#import "SVGKImage.h"
#import "SVGKParser.h"
@interface LGPatientSportsController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *evaButton;
/**   */
@property (nonatomic , strong)UIButton *planButton;
/**   */
@property (nonatomic , strong)UILabel *evaTitleLabel;
/**   */
@property (nonatomic , strong)UIButton *evaDetailBtn;

/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *detailBtn;
/**   */
@property (nonatomic , strong)LGPatientSportsChartView *chartView;
/**   */
@property (nonatomic , strong)NSArray *evaArray;
/**   */
@property (nonatomic , copy)NSString *planid;
/**   */
@property (nonatomic , copy)NSString *day;
@end

@implementation LGPatientSportsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.day = @"7";
    self.navigationView.title = @"运动康复";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:CGRectMake(0, kMaxY(self.evaButton.frame), Screen_W, kMaxY(self.chartView.frame)+viewPix(100)-kMaxY(self.evaButton.frame))];
    self.emptyView.backgroundColor = [UIColor clearColor];
    [self requestData];
    __weak typeof(self) wealSelf = self;
    self.chartView.checkDayRequest = ^(NSString * _Nonnull day) {
        wealSelf.day = day;
        [wealSelf requestData];
    };
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/sport.patient_plan/index" parameters:@{@"patient_id":self.patientid,@"days":self.day} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.planid = LGNSString(returnData[@"plan_id"]);
            self.chartView.dataDic = returnData[@"sport_record"][@"data"];
            NSMutableArray *tempArray = [LGEvaluationModel mj_objectArrayWithKeyValuesArray:returnData[@"evas"]];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                for (NSInteger i=0; i<tempArray.count; i++) {
                    LGEvaluationModel *model = tempArray[i];
                    model.image = [SVGKImage imageWithContentsOfURL:[NSURL URLWithString:model.icon]].UIImage;
                    [tempArray replaceObjectAtIndex:i withObject:model];
                }
                self.evaArray = tempArray;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.emptyView stopBufferAction];
                    [self.tableView reloadData];
                });
            });
        }
        if(self.chartView.dataDic.allKeys.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName2 content:@"暂无运动记录" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.chartView.dataDic.allKeys.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName2 content:@"暂无运动记录" offset:0];
        }
    }];
}

//运动评估、运动方案
-(void)itemButtonTouched:(UIButton *)sender{
    if(sender == self.evaButton){
        //运动评估
        LGSportsEvaRecordController *controller = [[LGSportsEvaRecordController alloc]init];
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else{
        //运动方案
        LGPatientSportsDetailController *controller = [[LGPatientSportsDetailController alloc]init];
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//查看全部的评估记录
-(void)checkMoreEvaReportAction{
    LGReportingCenterController *controller = [[LGReportingCenterController alloc]init];
    controller.patientid = self.patientid;
    controller.diseaseCode = self.diseaseCode;
    controller.currentIndex = 4;
    [self.navigationController pushViewController:controller animated:YES];
}

//查看详细记录
-(void)checkPlanDetailAction{
    LGSportsTrainRecordController *controller = [[LGSportsTrainRecordController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.evaArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPatientSportsEvaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.evaArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LGEvaluationModel *model = self.evaArray[indexPath.row];
    if(model.eva_date.length>0){
        LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
        controller.withAsset = YES;
        controller.urlStr = model.h5url;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientSportsEvaCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = viewPix(90);
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
        _tableView.tableFooterView = [self tableFooterView];
    }
    return _tableView;
}


-(UIView *)tableHeaderView{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(132))];
    headView.backgroundColor = [UIColor clearColor];
    self.evaButton = [self itemButton:@"运动评估" frame:CGRectMake(viewPix(16), viewPix(16), (Screen_W-viewPix(42))/2.0, viewPix(66))];
    self.planButton = [self itemButton:@"运动方案" frame:CGRectMake(Screen_W/2.0+viewPix(5), viewPix(16), (Screen_W-viewPix(42))/2.0, viewPix(66))];
    [headView addSubview:self.evaButton];
    [headView addSubview:self.planButton];
    [headView addSubview:self.evaTitleLabel];
    [headView addSubview:self.evaDetailBtn];
    return headView;
}

-(UIView *)tableFooterView{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(310))];
    footView.backgroundColor = [UIColor clearColor];
    [footView addSubview:self.titleLabel];
    [footView addSubview:self.detailBtn];
    [footView addSubview:self.chartView];
    
    return footView;
}

-(UILabel *)evaTitleLabel{
    if(!_evaTitleLabel){
        _evaTitleLabel = [UILabel lableWithText:@"量表评估记录" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _evaTitleLabel.frame = CGRectMake(viewPix(20), kMaxY(self.evaButton.frame)+viewPix(20), viewPix(120), viewPix(22));
    }
    return _evaTitleLabel;
}

-(UIButton *)evaDetailBtn{
    if(!_evaDetailBtn){
        _evaDetailBtn = [UIButton buttonWithTitle:@"查看全部" titleFont:LGFont(15) textColor:@"#666666" imageName:@"grayTip12"];
        _evaDetailBtn.frame = CGRectMake(Screen_W-viewPix(116), kMaxY(self.evaButton.frame)+viewPix(12), viewPix(100), viewPix(36));
        _evaDetailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_evaDetailBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:0];
        [_evaDetailBtn addTarget:self action:@selector(checkMoreEvaReportAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _evaDetailBtn;
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"运动训练记录" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(20), viewPix(12), viewPix(120), viewPix(22));
    }
    return _titleLabel;
}

-(UIButton *)detailBtn{
    if(!_detailBtn){
        _detailBtn = [UIButton buttonWithTitle:@"详细记录" titleFont:LGFont(15) textColor:@"#666666" imageName:@"grayTip12"];
        _detailBtn.frame = CGRectMake(Screen_W-viewPix(116), kMinY(self.titleLabel.frame)-viewPix(7), viewPix(100), viewPix(36));
        _detailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_detailBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:0];
        [_detailBtn addTarget:self action:@selector(checkPlanDetailAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailBtn;
}

-(LGPatientSportsChartView *)chartView{
    if(!_chartView){
        _chartView = [[LGPatientSportsChartView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(46), Screen_W-viewPix(32), viewPix(265))];
        [_chartView setShadoWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _chartView.backgroundColor = [UIColor whiteColor];
    }
    return _chartView;
}

-(UIButton *)itemButton:(NSString *)title frame:(CGRect)frame{
    UIButton *button = [UIButton buttonWithTitle:title titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#333333" imageName:[NSString stringWithFormat:@"运动方案-%@",title]];
    [button setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
    button.backgroundColor = [UIColor whiteColor];
    button.frame = frame;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(16), 0, 0);
    button.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(20), 0, 0);
    [button addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    [button addSubview:tipView];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(button).offset(-viewPix(16));
        make.width.height.equalTo(@(viewPix(12)));
        make.centerY.equalTo(button);
    }];
    
//    CAGradientLayer *layer = [CAGradientLayer layer];
//    layer.frame = CGRectMake(0, 0, (Screen_W-viewPix(42))/2.0, viewPix(66));
//    layer.colors = @[(__bridge id)[UIColor colorWithString:@"#E8F2FE"].CGColor, (__bridge id)[UIColor colorWithString:@"#FFFFFF"].CGColor];
//    layer.startPoint = CGPointMake(0.5, 0.13);
//    layer.endPoint = CGPointMake(0.5, 1);
//    layer.locations = @[@(0), @(1.0f)];
//    layer.cornerRadius = 15;
//    [button.layer addSublayer:layer];
//    [button bringSubviewToFront:button.imageView];
//    [button bringSubviewToFront:button.titleLabel];
//    [button bringSubviewToFront:tipView];
    return button;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
